package com.localShare.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

import com.ipackage.ContentLoad;
import com.localShare.Constant;
import com.localShare.R;
import com.localShare.load.SingleImageLoad;
import com.util.SharedPreferencesUtil;
import com.util.WindowManagerUtil;

/**
 * 单张图片加载显示
 */
public class SingleImageActivity extends BaseActivity{
    //首选项工具类
    private final SharedPreferencesUtil sharedPreferencesUtil=new SharedPreferencesUtil();
    //屏幕管理器工具类
    private final WindowManagerUtil windowManagerUtil=new WindowManagerUtil();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //锁屏时也可显示
        windowManagerUtil.showWhenLocked(this);
        //加载布局
        setContentView(R.layout.activity_single_image);
        //全屏
        windowManagerUtil.fullScreen(this);
        //用于显示图片的ImageView
        ImageView iv=(ImageView)findViewById(R.id.iv);
        //获取意图及flag，根据flag，判定是从当前Intent，还是从历史记录中加载数据
        Intent intent=getIntent();
        int flag=intent.getFlags();
        switch(flag){
            //从Intent中读取数据，并保存
            case Constant.COM_FROM_CURRENT:
                ContentLoad contentLoad=new SingleImageLoad();
                contentLoad.load(this,iv,intent);
                //图片路径存储放在了，第一次加载图片的方法中
                //LAST_LOAD_FLAG设置为LAST_LOAD_SINGLE_IMAGE
                sharedPreferencesUtil.saveInt(this,Constant.PRIVATE_SHARED_PREFERENCE,Constant.LAST_LOAD_FLAG,Constant.LAST_LOAD_SINGLE_IMAGE);
                break;
            //从历史记录读取数据
            case Constant.COM_FROM_HISTORY:
                //读取数据
                String uriString =sharedPreferencesUtil.getString(this,Constant.PRIVATE_SHARED_PREFERENCE,Constant.SINGLE_IMAGE_URI,Constant.SINGLE_IMAGE_URI_INIT);
                //数据不为空的话，加载
                if(!Constant.SINGLE_IMAGE_URI_INIT.equals(uriString)){
                    Uri uri=Uri.parse(uriString);
                    SingleImageLoad simpleImageLoad=new SingleImageLoad();
                    simpleImageLoad.load(this,iv,uri);
                }
                break;
        }
    }

}
